package com.yami.shop.discount.api.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.DiscountDto;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.enums.DiscountRule;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.discount.common.model.Discount;
import com.yami.shop.discount.common.model.DiscountItem;
import com.yami.shop.discount.common.service.DiscountService;
import com.yami.shop.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 促销活动接口
 * @author yami
 */
@RestController
@RequestMapping("/marking/discount")
@Tag(name = "促销活动接口")
public class DiscountController {

    @Autowired
    private DiscountService discountService;
    @Autowired
    private ProductService productService;

    @GetMapping("/getDiscountByProdId")
    @Operation(summary =  "通过商品id获取商品所有促销活动", description = "通过商品id获取商品所有促销活动")
    @Parameter(name = "prodId", description = "商品Id", required = true)
    public ServerResponseEntity<List<DiscountDto>> getDiscountByProdId(@RequestParam(name="prodId") Long prodId) {
        Product product = productService.getById(prodId);
        List<DiscountDto> discountDtoList = discountService.listByProdId(prodId,product.getShopId());
        return ServerResponseEntity.success(discountDtoList);
    }

    @GetMapping("/prodList")
    @Operation(summary =  "限时特惠(获取满减下的商品列表)", description = "获取限时特惠商品列表")
    @Parameter(name = "discountId", description = "活动ID", required = true)
    public ServerResponseEntity<IPage<ProductDto>> discountProdList(PageParam<ProductDto> page,@RequestParam(name="discountId") Long discountId) {
        IPage<ProductDto> productDtoPage = discountService.discountProdList(page,discountId);
        List<ProductDto> records = productDtoPage.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            Discount discount = discountService.getDiscountAndItemAndProdById(discountId);
            // 最优惠的那个满减项
            DiscountItem discountItem = discount.getDiscountItems().get(0);
            // 折扣百分比
            double discountProportion;
            // 打折
            if (Objects.equals(discount.getDiscountRule(), DiscountRule.M2D.value()) || Objects.equals(discount.getDiscountRule(), DiscountRule.P2D.value())) {
                discountProportion = Arith.sub(1,Arith.div(discountItem.getDiscount(), 10L));
            } else {
                discountProportion = Arith.div(discountItem.getDiscount(), discountItem.getNeedAmount());
            }
            for (ProductDto prod : records) {
                // 将要减去的价格
                double subPrice = Arith.mul(prod.getPrice(), discountProportion);
                // 如果有活动价格上限
                if (discount.getMaxReduceAmount() != null && subPrice > discount.getMaxReduceAmount()) {
                    subPrice = discount.getMaxReduceAmount();
                }
                //价格最低只能为0.01
                double activityPrice = Arith.sub(prod.getPrice(), subPrice);
                if (activityPrice<0.01){
                    activityPrice = 0.01;
                }
                prod.setActivityPrice(activityPrice);
            }
        }
        return ServerResponseEntity.success(productDtoPage);
    }

    @GetMapping("/getDiscountList")
    @Operation(summary =  "获取促销活动列表", description = "获取促销活动列表")
    public ServerResponseEntity<IPage<DiscountDto>> getDiscountList(PageParam<Discount> page) {
        IPage<DiscountDto> productDtoPage = discountService.getDiscountList(page);
        return ServerResponseEntity.success(productDtoPage);
    }

    @GetMapping("/getDiscountByDiscountId/{discountId}")
    @Operation(summary =  "获取促销活动详情", description = "通过活动id获取促销活动详情")
    @Parameter(name = "discountId", description = "活动ID", required = true)
    public ServerResponseEntity<DiscountDto> getDiscountByDiscountId(@PathVariable("discountId") Long discountId) {
        DiscountDto discountDto = discountService.getDiscountByDiscountId(discountId);
        if (Objects.isNull(discountDto)){
            // 该活动不存在或者已结束
            throw new YamiShopBindException("yami.activity.cannot.find.check");
        }
        return ServerResponseEntity.success(discountDto);
    }
}
